package com.cpi.cornpp.salesforcast.action;

import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.customer.exception.CustomerException;
import com.cpi.cornpp.customer.services.CustomerServices;
import com.cpi.cornpp.customer.view.CustomerView;
import com.cpi.cornpp.product.exception.ProductException;
import com.cpi.cornpp.product.services.ProductService;
import com.cpi.cornpp.product.view.ProductView;
import com.cpi.cornpp.salesforcast.exception.SalesForecastException;
import com.cpi.cornpp.salesforcast.form.SalesForecastForm;
import com.cpi.cornpp.salesforcast.helper.SalesForecastHelper;
import com.cpi.cornpp.salesforcast.service.SalesForecastService;
import com.cpi.cornpp.salesforcast.view.SalesForecastView;

public class SalesForecastAction extends MappingDispatchAction {
	private static final String YEARS_LABEL = "years";//$NON-NLS-1$
	private static final String DEFAULT_LABEL = "default";//$NON-NLS-1$
	private static final String NEW_LABEL = "new";//$NON-NLS-1$
	private static final String SEARCH_LABEL = "search";//$NON-NLS-1$
	private static final String SALES_FORECAST_IN_SESSION_LABEL = "SalesForecastView";//$NON-NLS-1$
	private static final String SUCCESS_UPDATE_LABEL = "common.edit.success";//$NON-NLS-1$
	private static final String MESSAGE = "MESSAGE";//$NON-NLS-1$
	private static final String PARAMETER_ID = "id";//$NON-NLS-1$
	private static final String CUSTOMER_VIEW = "customerView";//$NON-NLS-1$
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String PRODUCT_VIEW = "productView";//$NON-NLS-1$
	public ActionForward searchCustomer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws CustomerException {
		CustomerServices customerServices = null;
		CustomerView customerView = null;
		String customerId = null;
		HttpSession httpSession = null;
		try{
			httpSession = request.getSession();
			customerId = request.getParameter(PARAMETER_ID);
			customerServices = new CustomerServices();
			customerView = customerServices.getCustomerById(customerId);
			if( customerView != null ){
				request.setAttribute(CUSTOMER_VIEW, customerView);
			}else{
				request.removeAttribute(CUSTOMER_VIEW);
			}
		}catch (CustomerException customerException){
			throw customerException;
		}catch (Exception exception){
			CustomerException customerException = null;
			exception.printStackTrace(System.out);
			customerException= new CustomerException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_SELECT);
			httpSession.setAttribute(CUSTOM_EXCEPTION, customerException);
			throw customerException;
		}
		return mapping.findForward(DEFAULT_LABEL);
	}
	
	public ActionForward searchMaterial(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws ProductException {
		ProductService productService = null;
		ProductView productView = null;
		String productId = null;
		HttpSession httpSession = null;
		try{
			httpSession = request.getSession();
			productId = request.getParameter(PARAMETER_ID);
			productService = new ProductService();
			productView = productService.getProductById(productId);
			if( productView != null ){
				request.setAttribute(PRODUCT_VIEW, productView);
			}else{
				request.removeAttribute(PRODUCT_VIEW);
			}
		}catch( ProductException productException ){
			throw productException;
		}catch(Exception exception){
			ProductException productException = null;
			exception.printStackTrace(System.out);
			productException= new ProductException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_SELECT);
			httpSession.setAttribute(CUSTOM_EXCEPTION, productException);
			throw productException;
		}
		return mapping.findForward(DEFAULT_LABEL);
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws SalesForecastException{
		SalesForecastHelper salesForecastHelper = null;
		List years = null;
		Collection salesForecastViews = null;
		SalesForecastForm salesForecastForm = null;	
		Calendar cal_today = null;
		SalesForecastService salesForecastService = null;
		String newForecast = null;
		String searchForecast = null;
		SalesForecastView salesForecastView = null;
		HttpSession httpSession = null;
		CustomerView customerView = null;
		ProductView productView = null;
		try{
			newForecast = request.getParameter(NEW_LABEL);
			searchForecast = request.getParameter(SEARCH_LABEL); 
			salesForecastHelper = new SalesForecastHelper();
			years = salesForecastHelper.labelValueYears();
			salesForecastForm = (SalesForecastForm)form;
			cal_today = Calendar.getInstance();
			httpSession = request.getSession();
			salesForecastView = (SalesForecastView) httpSession.getAttribute(SALES_FORECAST_IN_SESSION_LABEL);		
			if(salesForecastView != null){
				if( newForecast != null ){
					salesForecastView = new SalesForecastView();
					httpSession.setAttribute(SALES_FORECAST_IN_SESSION_LABEL, salesForecastView);
				}
				salesForecastForm.setSalesForecastView(salesForecastView);
			}
			
			customerView = (CustomerView) httpSession.getAttribute("CustomerView");
			if(customerView != null){
				if( newForecast != null ){
					customerView = new CustomerView();
					httpSession.setAttribute("CustomerView", customerView);
				}
				salesForecastForm.getSalesForecastView().setCustomer(customerView.getCustomerId());
				salesForecastForm.getSalesForecastView().setCustomerDescription(customerView.getDescription());
			}
			
			productView = (ProductView) httpSession.getAttribute("ProductView");
			if(productView != null){
				if( newForecast != null ){
					productView = new ProductView();
					httpSession.setAttribute("ProductView", productView);
				}
				salesForecastForm.getSalesForecastView().setMaterial(productView.getProductId());
				salesForecastForm.getSalesForecastView().setMaterialDescription(productView.getDescription());
			}
			
			if( StringUtils.isEmpty(salesForecastForm.getSalesForecastView().getFiscalYear()) ){
				salesForecastForm.getSalesForecastView().setFiscalYear(String.valueOf(cal_today.get(Calendar.YEAR)));
			}
			
			if( searchForecast != null ){
				salesForecastService = new SalesForecastService();
				salesForecastViews = salesForecastService.getSalesForecastList(salesForecastForm.getSalesForecastView());
				salesForecastForm.setSalesForecastViews(salesForecastViews);
			}else{
				salesForecastForm.setSalesForecastViews(null);
			}
			
			request.setAttribute(YEARS_LABEL, years);
		}catch(SalesForecastException salesForecastException){
			throw salesForecastException;
		}catch(Exception exception){
			SalesForecastException salesForecastException = null;
			salesForecastException = new SalesForecastException(exception, CornPPException.LAYER_ACTION, CornPPException.ACTION_LISTS);
			exception.printStackTrace();
			throw salesForecastException;
		}
		return mapping.findForward(DEFAULT_LABEL);
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws SalesForecastException{
		SalesForecastService salesForecastService = null;
		SalesForecastForm salesForecastForm = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;
		try{
			httpSession = request.getSession();
			messageResources = getResources(request);
			salesForecastService = new SalesForecastService();
			salesForecastForm = (SalesForecastForm)form;
			salesForecastService.save((List)salesForecastForm.getSalesForecastViews());
			httpSession.removeAttribute("CustomerView");
			httpSession.removeAttribute("ProductView");
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_UPDATE_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch(SalesForecastException salesForecastException){
			throw salesForecastException;
		}catch(Exception exception){
			SalesForecastException salesForecastException = null;
			salesForecastException = new SalesForecastException(exception, CornPPException.LAYER_ACTION, CornPPException.ACTION_LISTS);
			exception.printStackTrace();
			throw salesForecastException;
		}
		return mapping.findForward(DEFAULT_LABEL);
	}
}